package services.book;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import repositories.AuthorRepository;
import services.GenericService;
import books.Author;
import dtos.AuthorDTO;

/**
 */
public class AuthorService extends GenericService {

    private static final long serialVersionUID = 1L;

    private AuthorRepository authorRepository;

    public void setAuthorRepository(final AuthorRepository authorRepository) {
        this.authorRepository = authorRepository;
    }

    public AuthorRepository getAuthorRepository() {
        return this.authorRepository;
    }

    @Transactional(readOnly = true)
    public List<AuthorDTO> getAllAuthorsDTO() {
        List<Author> authors = this.getAuthorRepository().findAll();
        return this.getFactoryDto().toDtos(AuthorDTO.class, authors);
    }

    @Transactional(readOnly = true)
    public List<Author> getAllAuthors() {
        return this.getAuthorRepository().findAll();
    }

    @Transactional
    public void addAuthor(final Author author) {
        this.getAuthorRepository().save(author);
    }

    @Transactional
    public void addAuthorDTO(final AuthorDTO authorDto) {
        Author author = this.getFactoryDto().toModel(Author.class, authorDto);
        this.getAuthorRepository().save(author);
    }

    @Transactional
    public Author findById(final Class<Author> class1, final long id) {
        return this.getAuthorRepository().findById(id);
    }

    @Transactional(readOnly = true)
    public List<Author> getAuthorsByName(final String name) {
        return this.getAuthorRepository().findAuthorByName(name);
    }

    @Transactional(readOnly = true)
    public List<AuthorDTO> getAuthorsByNameDTO(final String name) {
        return this.getFactoryDto().toDtos(AuthorDTO.class, this.getAuthorRepository().findAuthorByName(name));
    }
}
